package com.itheima.health.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.health.dto.CheckGroupDTO;
import com.itheima.health.entity.PageResult;
import com.itheima.health.entity.QueryPageBean;
import com.itheima.health.mapper.CheckGroupMapper;
import com.itheima.health.pojo.CheckGroup;
import com.itheima.health.pojo.CheckItem;
import com.itheima.health.service.CheckGroupService;
import com.itheima.health.service.CheckItemService;
import com.itheima.health.vo.CheckGroupVO;
import org.apache.commons.lang.StringUtils;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

//import com.itheima.health.pojo.User22;

/**
 * @program: health_paret
 * @description:
 * @author: Mr.Wang
 * @create: 2020-11-03 20:20
 **/
@Service
@Transactional
public class CheckGroupServiceImpl extends ServiceImpl<CheckGroupMapper, CheckGroup> implements CheckGroupService {
    //增加检查组信息
    @Override
    public Boolean saveCheckGroup(CheckGroupDTO checkGroupDTO) {
        //这里的话是将增加表单中的基本信息先给保存到数据库中
        Integer id = checkGroupDTO.getId();
        //这里是判断要不要删除中间表
        if (StringUtils.isNotBlank(String.valueOf(id))) {
            baseMapper.delete(id);
        }
        Integer gid = checkGroupDTO.getId();
        boolean save = this.saveOrUpdate(checkGroupDTO);
        //然后将我们dto中绑定的‘检查项’给附上值
        Integer[] checkitemIds = checkGroupDTO.getCheckitemIds();
        if (checkitemIds != null && checkitemIds.length > 0) {
            for (Integer iid : checkitemIds) {
                baseMapper.addCheckGroupandCheckitem(gid, iid);
            }
        }

        return true;
    }
    //


    //分页查询
    @Override
    public PageResult findPage(QueryPageBean queryPageBean) {
//select * from t_user where setIs_delete = 0
        // 查看是否删除把0和1作为参数传进去
        QueryWrapper<CheckGroup> wrapper = new QueryWrapper<CheckGroup>();
        wrapper.eq("Is_delete", 0);
        //这里进行判断是有条件查询还是无条件查询的
        Page<CheckGroup> page = null;
        if (!StringUtils.isBlank(queryPageBean.getQueryString())) {
            String queryString = queryPageBean.getQueryString();
            wrapper.like("helpcode", queryString).or().like("name", queryString).or().like("code", queryString);
            page = this.page(new Page<CheckGroup>(queryPageBean.getCurrentPage(), queryPageBean.getPageSize()), wrapper);
        } else {
            page = this.page(new Page<CheckGroup>(queryPageBean.getCurrentPage(), queryPageBean.getPageSize()), wrapper);
        }

        List<CheckGroup> records = page.getRecords();
        long total = page.getTotal();
        System.out.println("records = " + records);
        System.out.println("total = " + total);
        PageResult result = new PageResult(total, records);
        return result;
    }

    // 进行逻辑上的删除
    @Override
    public Boolean updateCheckGroupById(int id) {
        CheckGroup checkGroup = new CheckGroup();
        checkGroup.setId(id);
        checkGroup.setIs_delete(1);
        boolean b = this.updateById(checkGroup);
        System.out.println("b = " + b);
        return b;
    }

    // 这个的话主要是要回滚给前端的检查项
    @Autowired
    CheckItemService checkItemService;

    @Override
    public CheckGroupVO findCheckItemInfoByGroupId(int id) {
        int[] checkitemInfoCheckGroup = baseMapper.findCheckitemInfoCheckGroup(id);
        List<CheckItem> list = checkItemService.list();
        CheckGroupVO checkGroupVO = new CheckGroupVO();
        checkGroupVO.setCheckItemList(list);
        checkGroupVO.setCheckitemIds(checkitemInfoCheckGroup);
        return checkGroupVO;
    }
//
//    //进行添加检查项以及修改检查项
//    @Override
//    public Boolean add(CheckItem checkItem) {
//        //这个方法的话就是把增加和修改给整合了
//        boolean b = this.saveOrUpdate(checkItem);
//        return b;
//    }

}
